﻿--[[
   @brief    系统菜单
   @author   hqzheng
   @version  1.0
   @date     2017-03-17
--]]



main_menue_view = 
{
    menu_id = 0;
    menu_h = 0;
    have_recommend_update = false;
    -- 设置按钮红点的标识
    setting_red_dot_key = "setting_btn_for_color_mode";

    init = function (parent_id)
        main_menue_view.menu_id = parent_id;
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil == menu_ptr then
            return;
        end
        menu_ptr:RemoveAll();

        -- 系统设置item
        local setting_id = main_menue_view.add_nor_item(
            menu_ptr, LoadStr(ID_SYS_MENU_SETTING));
        ui_utility.setproperty_by_id(setting_id, "OnClick", "main_menue_view.on_click_setting_item");
        ui_utility.set_common_menu_top_item_bkg(setting_id);
        RedDotHelper.AddRedDot(setting_id, main_menue_view.setting_red_dot_key,
            "common\\red_dot.tpf/TenioUILayout/UIObject/Children/UIObject?CtnNewIcon");

        -- 工具箱item
        local tool_box_id = main_menue_view.add_nor_item(
            menu_ptr, LoadStr(ID_SYS_MENU_TOOLBOX));
        ui_utility.setproperty_by_id(tool_box_id, "OnClick", "main_menue_view.on_click_tool_box_item");
        ui_utility.set_common_menu_nor_item_bkg(tool_box_id);

        -- 添加游戏
        local add_game_id = main_menue_view.add_nor_item(
            menu_ptr, LoadStr(ID_SYS_MENU_ADDGAME));
        ui_utility.setproperty_by_id(add_game_id, "OnClick", "main_menue_view.on_click_add_game_item");
        ui_utility.set_common_menu_nor_item_bkg(add_game_id);

        -- 大屏模式
        local result = comm_center.call_service("i_service_bp_ui_request", {
            msg_type = "check_can_show_bp_entry",
        });
        if result ~= nil and result.rsp_param ~= nil and result.rsp_param == "1" and
           base_utility.is_develop_client() == false then
            local big_pic_id = main_menue_view.add_nor_item(
                menu_ptr, LoadStr("SYS_MENU_ITEM_BIG_PIC"));
            ui_utility.setproperty_by_id(
                big_pic_id, "OnClick", "main_menue_view.on_click_big_pic_item");
            ui_utility.set_common_menu_nor_item_bkg(big_pic_id);
        end

        -- 智能服务
--        if base_utility.is_type_of_oversea_ver() == false then
--            local ai_service_id = main_menue_view.add_nor_item(
--                menu_ptr, LoadStr(ID_SYS_MENU_AISERVICE));
--            ui_utility.setproperty_by_id(ai_service_id, "OnClick", "main_menue_view.on_click_ai_service");
--            ui_utility.set_common_menu_nor_item_bkg(ai_service_id);
--        end

        -- 切换分区
       -- local change_area_id = main_menue_view.add_nor_item(
       --     menu_ptr, LoadStr("CHANGE_STORE_AREA"));
        --    ui_utility.setproperty_by_id(change_area_id, "OnClick", "main_menue_view.on_click_change_area");
        --    ui_utility.set_common_menu_nor_item_bkg(change_area_id);

        -- 离线模式
        local game_id_rsp = comm_center.call_service("Svr_GetMultiWeGameLaunchGameID", {});
        local is_support_offline = true;
        if nil ~= game_id_rsp and 0 ~= game_id_rsp.game_id then
            is_support_offline = false;
        end
        local game_lib =
        base_utility.get_component_adapter("IGame_library",
          "ierd_tgp.game_library.Get_game_library_adapter();");
        local channel_id = game_lib:get_bind_channel_id();
        if 0 == channel_id and true == is_support_offline then
            local offline_id = main_menue_view.add_nor_item(
                menu_ptr, LoadStr(ID_SYS_MENU_OFFLINE));
            ui_utility.setproperty_by_id(offline_id, "OnClick", "main_menue_view.on_click_offline_item");
            ui_utility.set_common_menu_nor_item_bkg(offline_id);

            -- 只有离线模式开启才可用
            local     item_ptr = ui_utility.get_ctrl_ptr_by_id(offline_id);

            if nil ~= item_ptr then
                local param = comm_center.call_service("Svr_IsOfflineOpen", {});

                if nil ~= param and 0 ~= param["open"] then
                    item_ptr:Enable(TRUE);
                else
                    item_ptr:Enable(FALSE);
                end;
            end;
        end
        

        -- 意见反馈item
        local advice_id = main_menue_view.add_nor_item(
            menu_ptr, LoadStr(ID_SYS_MENU_ADVICE));
        ui_utility.setproperty_by_id(advice_id, "OnClick", "main_menue_view.on_click_advice_item");
        ui_utility.set_common_menu_nor_item_bkg(advice_id);

        -- 隐私协议
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            local privacy_id = main_menue_view.add_nor_item(
                menu_ptr, LoadStr("SYS_MENU_ITEM_POLICY"));
            ui_utility.setproperty_by_id(privacy_id, "OnClick", "main_menue_view.on_click_privacy_item");
            ui_utility.set_common_menu_nor_item_bkg(privacy_id);
        end

        -- 关于我们item
        local new_icon = "ONEIMAGE:client_mainframe\\sys_menu\\new_flag.png;Stretch;0";
        local about_id = main_menue_view.add_nor_item(
            menu_ptr, LoadStr(ID_SYS_MENU_ABOUT), new_icon);
        ui_utility.setproperty_by_id(about_id, "OnClick", "main_menue_view.on_click_about_item");
        ui_utility.set_common_menu_nor_item_bkg(about_id);
        main_menue_view.show_recommend_tips(about_id);

        -- 版本修复item, 目前只支持国内版/个人版
        if base_utility.CLIENT_TYPE.CHINA_VER == base_utility.get_client_version_type() and
           not base_utility.is_netbar_ver() and not base_utility.is_develop_client() then
            local repair_id = main_menue_view.add_nor_item(
                menu_ptr, LoadStr(ID_SYS_MENU_REPAIR));
            ui_utility.setproperty_by_id(repair_id, "OnClick", "main_menue_view.on_click_repair_item");
            ui_utility.set_common_menu_nor_item_bkg(repair_id);
        end

        -- 退出item
        local quit_tgp_id = main_menue_view.add_nor_item(
            menu_ptr, LoadStr(ID_SYS_MENU_QUIT_TGP));
        ui_utility.setproperty_by_id(quit_tgp_id, "OnClick", "main_menue_view.on_click_quit_tgp_item");
        ui_utility.set_common_menu_bottom_item_bkg(quit_tgp_id);
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if nil ~= menu_ptr then
            local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);
            main_menue_view.menu_h = menu_rc.bottom - menu_rc.top;
        end
    end;

    show = function (parent_id, is_show, off_set_x, off_set_y)
        main_menue_view.init(parent_id);
        if is_show then
            -- 移动到要显示的位置
            local menu_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
            if nil ~= menu_ptr then
                local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);
                local menu_w = menu_rc.right - menu_rc.left;
                local menu_h = menu_rc.bottom - menu_rc.top;
                menu_rc.left = off_set_x - 10;
                menu_rc.right = menu_rc.left + menu_w;
                menu_rc.top = off_set_y;
                menu_rc.bottom = menu_rc.top;

                menu_ptr:MoveEx(menu_rc,TRUE);
            end
            main_menue_view.show_animation(parent_id);
        else
            main_menue_view.hide_animation(parent_id);
        end
    end;

    do_recommend_update = function()
        main_menue_view.have_recommend_update = true;
        -- main_menue_view.show_recommend_tips();
    end;

    show_recommend_tips = function(about_id)
        local new_id = ui_utility.get_ctrl_id_by_path(about_id, "Ctn_Icon");
        if main_menue_view.have_recommend_update then
            ui_utility.setproperty_by_id(new_id, "Visible", "true");
        else
            ui_utility.setproperty_by_id(new_id, "Visible", "false");
        end
    end;

    add_nor_item = function (menu_ptr, item_name, icon)
        local item_id = 0;
        if nil ~= menu_ptr then
            local const_ui_str = U8toCUIStr("wegame_mainframe\\MenuItem.tpf/TenioUILayout/UIObject/Children/UIObject?btnCfgItem");
            item_id = menu_ptr:AppendItem(const_ui_str);
            local language_ver = base_utility.get_current_language();
            if base_utility.LANG_TYPE.ZH_CN == language_ver or 
                base_utility.LANG_TYPE.ZH_HK == language_ver then
                ui_utility.setproperty_by_id(item_id, "AlignMode", "CENTER");
            else
                ui_utility.setproperty_by_id(item_id, "AlignMode", "LEFT");
                ui_utility.setproperty_by_id(item_id, "TextOffsetX", "12");
            end;
            ui_utility.setproperty_by_id(item_id, "Caption", item_name);
            local item_icon_id = ui_utility.get_ctrl_id_by_path(item_id, "Ctn_Icon");
            ui_utility.setproperty_by_id(item_icon_id, "Background", icon);
        end
        return item_id;
    end;

    on_click_big_pic_item = function()
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "big_picture");
        comm_center.call_service("i_service_req_enter_bp_mode", {source=1});
    end;

    on_click_setting_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "sys_setting");
        local lua_table_para = {tab_id = 0};
        local uilib = ui_utility.uilib();
        local json_table_para = base_utility.table_to_json(lua_table_para);
        event_center.send_event(main_menue_event.MAIN_MENUE_SETTING, json_table_para);
        main_menue_view.hide_animation(main_menue_view.menu_id);
        RedDotHelper.ClearRedDot(main_menue_view.setting_red_dot_key);
    end;

    on_click_tool_box_item = function (uilib, control_id)
        base_utility.log("[toolbox] click menue item");
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "tool_box");
        local param ={};
        param["type"] = "tools";
        param["sub_page"] = "NET_ACCE";
        param["game_id"] = 0;
        web_utility.open_link(base_utility.table_to_json(param));
        tqos_mgr.remember_btn_clicked("MAINWINDOW_TOOL_BTN");
        main_menue_view.hide_animation(main_menue_view.menu_id);
    end;

    on_click_add_game_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "add_game");       
        event_center.send_event(main_menue_event.MAIN_MENUE_ADD_GAME, "");
        main_menue_view.hide_animation(main_menue_view.menu_id);
    end;

    on_click_change_area = function (uilib, control_id)
        comm_center.call_service("SvrShowChangearea", {});
        main_menue_view.hide_animation(main_menue_view.menu_id);
    end;

    on_click_ai_service = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "ai_service");
        local info = comm_center.call_service("i_servie_get_my_tgp_id", {});
        local tgpid = 0;
        if nil ~= info then
            tgpid = info.tgp_id;
        end;
        -- 这里用心悦的服务，因为心悦的才是用ptlogin跳转的.
        comm_center.call_service("SvrShowXinYue", { url=LoadStr("AI_SERVICE_URL")..tgpid, width=712, height=533, title=LoadStr(ID_SYS_MENU_AISERVICE), canWebClose=true, FixSize=true});
    end;

    on_click_advice_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "feedback");
        comm_center.call_service("i_service_feedback_show", {gameid=0, jumpfrom=1});
        main_menue_view.hide_animation(main_menue_view.menu_id);
    end;

    on_click_update_item = function (uilib, control_id)
        --上报qos:点击升级按钮
        main_menue_view.report_click_url(20);
        if main_menue_view.have_recommend_update then
            event_center.send_event(main_menue_event.MAIN_MENUE_UPDATE_TIPS, "");
        else
            event_center.send_event(main_menue_event.MAIN_MENUE_TGP_UPDATE, "");
        end
    end;

    on_click_about_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "about_us");
        event_center.send_event(main_menue_event.MAIN_MENUE_ABOUT, "");
        main_menue_view.hide_animation(main_menue_view.menu_id);
    end;

    on_click_repair_item = function(uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "repair_version");
        local zParam = 
        {
            position = "center",
            title = LoadStr("REPAIR_WEGAME_TITLE"),
            width = 480,
            height = 230,
            titleIcon = "",
            icon = "",
            tipsIcon = poptips_icons.kWarning,
            descTitle = LoadStr("REPAIR_WEGAME_CONTENT"),
            desc = LoadStr("REPAIR_WEGAME_CONTENT_DESC"),
            parent_id = main_id,
            modal = false,
            always_center = true,
            close_means_cancel = false,
            okBtn = 
            {
                visible = "true",
                close_win = true,
                text = LoadStr("REPAIR_WEGAME_CUR_VER"),
                handle = function()
                    local game_id = GAME_ID.GAME_ID_TGP;
                    comm_center.call_service("i_open_repair_game_wnd", 
                                            { game_id = game_id,
                                              latest = false
                                             });
                end
            },
            cancelBtn = 
            {
                visible = "false",    
            },
            extraBtn = 
            {
                visible = "true",
                close_win = true,
                text = LoadStr("REPAIR_WEGAME_LATEST_VER"),
                handle = function()
                    local game_id = GAME_ID.GAME_ID_TGP;
                    comm_center.call_service("i_open_repair_game_wnd", 
                                            { game_id = game_id,
                                              latest = true
                                             });
                end
            }
       };

       poptips_control.show_tips(zParam);
       main_menue_view.hide_animation(main_menue_view.menu_id);
    end;

    on_click_offline_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "offline");

        local zRtn =  comm_center.call_service("Svr_CanOffline", {});

        -- 判断一下是否可以离线登录
        if (nil ~= zRtn) and (0 ~= zRtn["can_offline"]) then
            local param = {};
            param["account_id"] = login_model.main_tgp_id;
            local json_str = base_utility.table_to_json(param);
            event_center.send_event(main_menue_event.MAIN_MENUE_SITCH_OFFLINE, json_str);
            main_menue_view.report_switch_offline("switch_offline_mode");
        else
            local nMainID = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);

            local zParam = 
            {
                position = "center",
                title = LoadStr("OFFLINE_WND_TITLE"),
                width = 480,
                height = 230,
                titleIcon = "",
                icon = "",
                tipsIcon = poptips_icons.kError,
                descTitle = LoadStr("OFFLINE_TIP_DESC_TITLE"),
                desc = LoadStr("OFFLINE_TIP_DESC_BODY"),
                parent_id = main_id,
                modal = false,
                always_center = true,
                okBtn = 
                {
                    visible = "true",
                    text = LoadStr("OFFLINE_WND_OK"),
                    close_win = true,
                    handle = function()
                    end
                },
                cancelBtn = 
                {
                    visible = "false",
                    text = LoadStr("OFFLINE_WND_CANCEL"),
                    handle = function()
                    end
                }
           };

           poptips_control.show_tips(zParam);
        end;
        main_menue_view.hide_animation(main_menue_view.menu_id);
    end;

    on_click_privacy_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "privacy");
        web_utility.open_browser(LoadStr("PRIVACY_URL"), false, true);
        main_menue_view.hide_animation(main_menue_view.menu_id);
    end;

    on_click_quit_tgp_item = function (uilib, control_id)
        base_utility.mainframe_report("platform_menu", "sys_menu_click", "quit");
        event_center.send_event(main_menue_event.MAIN_MENUE_EXIT_TGP, "");
    end;

    show_animation = function (menu_id)
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        if nil == menu_ptr then
            return;
        end

        menu_ptr:Show(TRUE);

        local menu_rc = menu_ptr:GetRectEx(TRUE,TRUE);

        -- 开始尺寸
        local resize_start_sz = SIZE:new_local();
        resize_start_sz.cx = menu_rc.right - menu_rc.left;
        resize_start_sz.cy = 0;

        -- 展开后的尺寸
        local resize_end_sz = SIZE:new_local();
        resize_end_sz.cx = menu_rc.right - menu_rc.left;
        resize_end_sz.cy = main_menue_view.menu_h;


        -- 缩放动画
        animation_utility.init(
            main_menue_view.menu_id,
            "Setting_menu_Resize", animation_utility.ANI_TYPE.ANI_SIZE);
        animation_utility.size_animation(
            main_menue_view.menu_id, resize_start_sz, resize_end_sz, 1, 0);

    end;

    hide_animation = function (menu_id)
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        if nil ~= menu_ptr then
            menu_ptr:Show(FALSE);
        end
    end;

    on_resize_animation_end = function ()
    end;

    report_click_url = function(action)
        local qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
        qosparam.oc_type = ierd_tgp.tpf_ui.kInstant;
        qosparam.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kUrlClick; --url上报
        qosparam.int_count = 3;
        qosparam.str_count = 0;
        qosparam.int_val[0]= action;
        qosparam.int_val[1]= 0;
        qosparam.int_val[2]= 0;
        base_utility.lua_qos(qosparam);
        qosparam:delete();
    end;

    report_switch_offline = function(sub_action)
        local report_data = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local kv_data = {};
        if nil ~= report_data then
            kv_data["result"] = "0";
            report_data.oc_type = 1;
            report_data.qos_kind = ierd_tgp.adapt_for_imports.qos.Qos_kind.kUrlClick;
            report_data.str_action = "offline_login"
            report_data.str_type = sub_action;
            report_data.str_json = base_utility.table_to_json(kv_data);
            report_data.game_id = 0;
            
            base_utility.lua_qos_ex(report_data);
            report_data:delete();
        end
    end;
};
